/**
 * 2017年10月13日
 */
package exp.algorithm.sic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import exp.util.MatrixUtil;
import weka.core.Instance;
import weka.core.Instances;

/**
 * sift算法的接收参数类型为TimeSeries weka里面的Instances类型太臃肿
 * 只需要特征检测的话不需要那么多方法进行操作.
 * @author Alex
 *
 */
public class TimeSeries {
	//data不包含类标,即data全是属性特征,类标使用classVal存储
	public float[] data;
	public double classVal = -1.0;
	//时间序列都是1 
	public int height =1;
	//宽度是序列长度
	public int width;
	public TimeSeries(double []d){
		data = MatrixUtil.doubleArrayToFloatArray(d);
		width = data.length;
	}
	public TimeSeries(int width,int height){
		this.width = width;
		data = new float[width];
	}
	public TimeSeries(float []d){
		this.data = new float[d.length];
		System.arraycopy(d, 0, this.data, 0, d.length);
		width = d.length;
	}
	
	public TimeSeries() {
		width = 0;
	}
	public TimeSeries(TimeSeries ts) {
		this.data = new float[ts.length()];
		width = ts.width;
	}
	public int length(){
		return width;
	}
	//左移位
	public void leftShift(){
		float temp = data[0];
		for(int i=1;i<data.length;i++)
			data[i-1]=data[i];
		data[data.length-1]=temp;
	}
	//右移位
	public void rightShift(){
		float temp = data[data.length-1];
		for(int i=data.length-2;i>=0;i--)
			data[i+1]=data[i];
		data[0]=temp;
	}
	public String toString(){
		return Arrays.toString(this.data);
	}
	public TimeSeries halved(){
		int width = this.width/2;
		if(width == 0) return null;
		TimeSeries haved = new TimeSeries(width,0);
		for(int i =0;i<haved.width;i++){
			haved.data[i]=this.data[i*2];
		}
		return haved;
	}
	public TimeSeries clone(){
		TimeSeries t = new TimeSeries(this.data);
		return t;
	}
	public static TimeSeries minus(TimeSeries t1,TimeSeries t2){
		TimeSeries res = t1.clone();
		for(int i=0;i<res.data.length;i++){
			res.data[i] -= t2.data[i];
		}
		return res;
		
	}
	public static TimeSeries fromInstance(Instance inst){
		double [] d = inst.toDoubleArray();
		double atts[] = new double[d.length-1];
		System.arraycopy(d, 0, atts, 0, atts.length);
		TimeSeries ts = new TimeSeries(atts);
		
		ts.classVal = inst.classValue();
		return ts;
	}
	public static List<TimeSeries> fromInstance(Instances inst){
		ArrayList<TimeSeries> l = new ArrayList<>();
		for(int i=0,total=inst.numInstances();i<total;i++){
			l.add(fromInstance(inst.get(i)));
		}
		return l;
	}
	public float[] getData() {
		return data;
	}
	public void setData(float[] data) {
		this.data = data;
	}
	
}
